package yanyan.com.tochar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.HttpUtil;
import yanyan.com.tochar.utils.StringUtil;
import yanyan.com.tochar.utils.ToastUtil;

public class FunsIpSelectActivity extends Activity {

    private EditText addressEd;
    private TextView resEd;
    private ProgressDialog loadin;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_ip);
        addressEd=findViewById(R.id.ip_address);
        resEd=findViewById(R.id.ip_res);

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_ip_select");


    }

    private void ipSelect(String ip){
        loadin=new ProgressDialog(this);
        loadin.setMessage("正在查询...");
        loadin.show();
        Call msg = HttpUtil.getMsg("http://m.tool.chinaz.com/ipsel?ip=" + ip);
        msg.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                loadin.cancel();
                ToastUtil.showShotToast(FunsIpSelectActivity.this, "出错了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                loadin.cancel();
                String res = response.body().string();
                String aa = res;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                     String str=  StringUtil.getMiddleStr(aa,"<table class=\"table mb0\">"," </tbody>");
                     str=StringUtil.getMiddleStr(str," <tbody>","");
                     str=StringUtil.getMiddleStr(str,"<tr>","</tr>");
                        List<String> middleStrList = StringUtil.getMiddleStrList(str, "<td", "</td>");
                        String res="";
                        for(int i=0;i<middleStrList.size();i++){
                            String s=middleStrList.get(i);
                            s=s.substring(s.indexOf("\">")+2);
                            middleStrList.set(i,s);
                        }
                        if(middleStrList.size()>2){
                            res="物理地址:"+middleStrList.get(0);
                            res=res+"\nip地址:"+middleStrList.get(1);
                            res=res+"\n数字地址:"+middleStrList.get(2);
                        }
                        if(res.length()<1){
                            res="未查询到地址";
                        }
                        resEd.setText(res);

                    }
                });
            }

        });
    }


    public void getIpAddress(View view){
        String ip=addressEd.getText().toString();
            if(ip.length()<1){
                ToastUtil.showLongToast(this,"请输入IP地址");
                return;
            }
            ipSelect(ip);
    }

}
